package com.foton.logistic.service;

import android.content.Context;

import com.foton.logistic.R;
import com.foton.logistic.model.order.LocationEntity;
import com.foton.logistic.model.order.OrderConditionEntity;
import com.foton.logistic.model.order.UploadTrackEntity;
import com.foton.logistic.util.OptionUtil;
import com.foton.logistic.util.TimeUtil;
import com.foton.logistic.util.database.DataBaseHelper;
import com.foton.logistic.util.tool.StringUtil;
import com.j256.ormlite.dao.Dao;

import java.util.ArrayList;
import java.util.List;

public class LocationService {
    private Context context;
    private DataBaseHelper dataBaseHelper;
    private Dao<LocationEntity, String> locationDao;
    private Dao<OrderConditionEntity, String> orderConditionDao;
    private static LocationService locationService = null;

    public static synchronized LocationService getInstance(Context context){
        if (locationService==null){
            locationService = new LocationService(context);
        }
        return locationService;
    }

    private LocationService(Context context) {
        this.context = context;
        init();
    }

    private void init(){
        try {
            dataBaseHelper = DataBaseHelper.getHelper(context);
            locationDao = dataBaseHelper.getLocationDao();
            orderConditionDao=dataBaseHelper.getOrderConditionDao();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 创建设备断开连接
     * @param
     */
    public void insert0(OrderConditionEntity orderConditionEntity){
        try {
            orderConditionDao.create(orderConditionEntity);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void insert1(OrderConditionEntity orderConditionEntity){
        try {
            orderConditionDao.createIfNotExists(orderConditionEntity);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 创建新定位点
     * @param locationEntity
     */
    public void insert(LocationEntity locationEntity){
        try {
            locationEntity.setId(OptionUtil.getUuid());
            locationEntity.setCreate_time(TimeUtil.getStamp());
            locationEntity.setUpdate_time(TimeUtil.getStamp());
            locationDao.createIfNotExists(locationEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 更新定位点信息
     * @param locationEntity
     */
    public void update(LocationEntity locationEntity){
        try {
            locationDao.update(locationEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 删除定位点
     * @param locationEntity
     */
    public void delete(LocationEntity locationEntity){
        try {
            locationDao.delete(locationEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 根据type查询6断开7连接
     * @param
     * @return
     */
    public  List<OrderConditionEntity> query1(String type){
        List<OrderConditionEntity> OrderConditionEntityList = new ArrayList<>();
        try {
            OrderConditionEntityList = (List<OrderConditionEntity>) orderConditionDao.queryBuilder().orderBy("time",false).where().eq("type",type).query();
        }catch (Exception e){
            e.printStackTrace();
        }
        return OrderConditionEntityList;
    }

    /**
     * 根据本地订单Id查询定位点
     * @param localId 本地订单Id
     * @return
     */
    public List<LocationEntity> query(String localId){
        List<LocationEntity> locationEntityList = null;
        try {
            locationEntityList = locationDao.queryBuilder().orderBy("update_time", true).where().eq("localId", localId).and().eq("result", "1").query();
        }catch (Exception e){
            e.printStackTrace();
        }
        return locationEntityList;
    }

    /**
     * 判断是否已经有起点或者终点
     * @param localId
     * @param startOrStop
     * @return
     */
    public boolean hasStartOrStop(String localId,boolean startOrStop){
        boolean hasStartOrStop = false;
        List<LocationEntity> locationEntityList = null;
        try {
            if (startOrStop){
                locationEntityList =  locationDao.queryBuilder().orderBy("update_time", true).where().eq("localId", localId).and().eq("msg", context.getString(R.string.loc_start)).query();
            }else {
                locationEntityList =  locationDao.queryBuilder().orderBy("update_time", true).where().eq("localId", localId).and().eq("msg",context.getString(R.string.loc_stop)).query();
            }
            if (locationEntityList!=null&&locationEntityList.size()>0){
                hasStartOrStop = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return hasStartOrStop;
    }

    /**
     * 根据订单编号和成功值查询定位点
     * @param localId 本地订单Id
     * @param result 成功值
     * @return
     */
    public List<LocationEntity> query(String localId, String result){
        List<LocationEntity> locationEntityList = null;
        try {
            if (StringUtil.isEmpty(result)){
                locationEntityList =  locationDao.queryBuilder().orderBy("update_time", true).where().eq("localId", localId).query();
            }else {
                locationEntityList =  locationDao.queryBuilder().orderBy("update_time", true).where().eq("localId", localId).and().eq("result",result).query();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return locationEntityList;
    }

    public List<LocationEntity> queryByOrderId(String localId){
        List<LocationEntity> locationEntityList = null;
        try {
            locationEntityList =  locationDao.queryBuilder().orderBy("update_time", true).where().eq("localId", localId).and().eq("isUploaded","0").and().eq("result","1").query();
        }catch (Exception e){
            e.printStackTrace();
        }
        return locationEntityList;
    }

    /**
     * 查询订单的轨迹
     * @param localId
     * @param desc 是否时间顺序
     * @return
     */
    public List<LocationEntity> queryByOrderId(String localId, boolean desc){
        List<LocationEntity> locationEntityList = null;
        try {
            locationEntityList =  locationDao.queryBuilder().orderBy("update_time", desc).where().eq("localId", localId).and().eq("result","1").query();
        }catch (Exception e){
            e.printStackTrace();
        }
        return locationEntityList;
    }

    /**
     * 查询订单的轨迹
     * @param desc 是否时间顺序
     * @return
     */
    public List<LocationEntity> queryByOrderId(boolean desc){
        List<LocationEntity> locationEntityList = null;
        try {
            locationEntityList =  locationDao.queryBuilder().orderBy("update_time", desc).where().eq("result","1").query();
        }catch (Exception e){
            e.printStackTrace();
        }
        return locationEntityList;
    }

    /**
     * 查询倒数4条数据
     * @param localId
     * @param result
     * @return
     */
    public List<LocationEntity> queryLimit(String localId, String result){
        List<LocationEntity> locationEntityList = null;
        try {
            if (StringUtil.isEmpty(result)){
                locationEntityList =  locationDao.queryBuilder().orderBy("update_time", false).limit(4).where().eq("localId", localId).query();
            }else {
                locationEntityList =  locationDao.queryBuilder().orderBy("update_time", false).limit(4).where().eq("localId", localId).and().eq("result",result).query();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return locationEntityList;
    }

    public void update2Uploaded(List<LocationEntity> list){
        try {
            for (LocationEntity locationEntity : list){
                locationDao.update(locationEntity);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 根据本地订单ID删除定位点
     * @param localId 订单编号
     * @return
     */
    public void deleteByLocalId(String localId){
        try {
            List<LocationEntity> locationEntityList = query(localId,"");
            locationDao.delete(locationEntityList);
//            for (LocationEntity locationEntity:locationEntityList){
//                locationDao.delete(locationEntity);
//            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 上传轨迹查询定位点 经度，纬度
     * @param localId 本地订单Id
     * @return
     */
    public UploadTrackEntity queryLocationList(String localId){
        UploadTrackEntity uploadTrackEntity = new UploadTrackEntity();
        List<LocationEntity> locationEntityList = query(localId);
        String is_moblie_track = "";
        for (int i=0;i<locationEntityList.size();i++){
            if(i==0){
                uploadTrackEntity.setStartTime(TimeUtil.formatServiceTime2(locationEntityList.get(i).getUpdate_time()));
                Double geoLat = locationEntityList.get(i).getLatDouble() * 1E6;
                Double geoLng = locationEntityList.get(i).getLngDouble() * 1E6;
                is_moblie_track = geoLng.intValue()+","+geoLat.intValue()+"";
            }else {
                Double geoLat = locationEntityList.get(i).getLatDouble() * 1E6;
                Double geoLng = locationEntityList.get(i).getLngDouble() * 1E6;
                is_moblie_track = is_moblie_track+";"+geoLng.intValue()+","+geoLat.intValue();
            }
            if (i==(locationEntityList.size()-1)){
                uploadTrackEntity.setEndTime(TimeUtil.formatServiceTime2(locationEntityList.get(i).getUpdate_time()));
            }
        }

        uploadTrackEntity.setLonlat_list(is_moblie_track);
        return uploadTrackEntity;
    }

}